/**
 * 
 */
package com.seguriapp.activities;

import java.util.Arrays;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.seguriapp.R;
import com.seguriapp.dialogs.UserInformationDialog;
import com.seguriapp.util.AppPrefences;

/**
 * @author pjohnson
 * 
 */
public class SettingsActivity extends Activity {

	private CheckBox chkGps;
	private CheckBox chkFb;
	private CheckBox chkTw;
	private Button btnPersonalInf;
	private Button btnAbout;
	private SettingsActivity mContext;
	private LoginButton facebookLoginButton;
	private UiLifecycleHelper uiHelper;

	private Session.StatusCallback callback = new Session.StatusCallback() {
		@Override
		public void call(Session session, SessionState state,
				Exception exception) {
			onSessionStateChange(session, state, exception);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		uiHelper = new UiLifecycleHelper(this, callback);
		uiHelper.onCreate(savedInstanceState);
		mContext = this;
		btnPersonalInf = (Button) findViewById(R.id.btnPersonalInf);
		btnAbout = (Button) findViewById(R.id.btnAbout);
		chkGps = (CheckBox) findViewById(R.id.chkGps);
		chkFb = (CheckBox) findViewById(R.id.chkFb);
		chkTw = (CheckBox) findViewById(R.id.chkTw);

		final AppPrefences msp = new AppPrefences(this);

		chkGps.setChecked(msp.isAlwaysGps());
		chkFb.setChecked(msp.isAlwaysFb());
		chkTw.setChecked(msp.isAlwaysTw());

		chkGps.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				msp.setAlwaysGps(chkGps.isChecked());
			}
		});

		chkFb.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				msp.setAlwaysFb(chkFb.isChecked());
			}
		});

		chkTw.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				msp.setAlwaysTw(chkTw.isChecked());
			}
		});

		btnPersonalInf.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				UserInformationDialog.createDialog(mContext).show();
			}
		});

		facebookLoginButton = (LoginButton) findViewById(R.id.login_button);
		facebookLoginButton.setReadPermissions(Arrays.asList("email"));

		facebookLoginButton
				.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
					@Override
					public void onUserInfoFetched(GraphUser user) {
						if (user != null) {

						}

					}

				});
	}

	protected void onSessionStateChange(Session session, SessionState state,
			Exception exception) {
	}

	@Override
	protected void onResume() {
		super.onResume();
		uiHelper.onResume();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		uiHelper.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onPause() {
		super.onPause();
		uiHelper.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		uiHelper.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		uiHelper.onSaveInstanceState(outState);
	}

}
